import { getToken } from './token.js';

const BASE_URL = 'https://changban.dianjingkeji.net/prod-api'
//const BASE_URL = 'http://192.168.10.129:8989';




function req(obj) {
	return new Promise((resolve, reject) => {
		const method = obj.method || 'GET'
		const path = obj.url.startsWith('/') ? obj.url : '/' + obj.url
		const url = BASE_URL + path
		const data = obj.data || {}
  
		const header = {
		 'Authorization': 'Bearer '+ getToken()
		}
		
		
		uni.request({
			url,
			data,
			method,
			header,
			success: ((res) => {
				
				if (res.data.code === 403 || res.data.code === 401) {
					// uni.showToast({
					// 	title: '未登录',
					// 	icon: 'none',
					// 	duration: 2000
					// });
					
					// // 错误处理，返回登录页
			        uni.clearStorageSync('token')
					   uni.clearStorageSync('phone')
					   uni.showModal({
					   	title: '提示',
					   	content: '是否授权登录体验完整功能？',
					   	success: function (res) {
					   		if (res.confirm) {
					   			uni.navigateTo({
					   				url:'/pages/login/login'
					   			})
					   		} else if (res.cancel) {
					   			console.log('用户点击取消');
					   		}
					   	}
					   });

					
				
				} else if (res.data.code != null && res.data.code === 500) {
					if(res.data.msg=='当前会员未设置个人信息，请前往我的资料进行设置'){
						resolve(res)	
					}
					uni.showToast({
						title: res.data.msg,
						icon: 'none',
						duration: 2000
					});
				}
				if (res.data.code === 200) {
					resolve(res)
				}
			}),
			fail: ((err) => {
				reject(err)
			})
		})
	})
}
export default req
